package com.example.annasbook.readers;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.example.annasbook.model.BookModel;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RList;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.batch.item.support.AbstractItemStreamItemReader;

import java.util.List;

@Slf4j
public class BookRedisListReader extends AbstractItemStreamItemReader<BookModel> {
    private final String BookListRedisKey = "annasList";
    private final RedissonClient redissonClient;

    public BookRedisListReader(@NonNull RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public BookModel read() {
        RQueue<String> data = redissonClient.getQueue(BookListRedisKey,new StringCodec());
        String poll = data.poll();
        try {
            return JSONObject.parseObject(poll,BookModel.class);
        }catch (Exception e){
            log.info("error data: {}",poll);
            log.error("",e);
            return null;
        }
    }
}
